#include "module_app_wifi.h"
#include "module_app_interface.h"
#include "module_wifi.h"
#include "esp_log.h"

static const char* TAG = "module_app_wifi";
static void full_wifi_list_ui(lv_obj_t *wifi_list, wifi_scan_result_t *wifi_scan_result);

static lv_obj_t *s_wifi_obj = NULL;
static lv_obj_t *s_wifi_list = NULL;

static void _wifi_page_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    char *txt = lv_event_get_user_data(e);
    wifi_scan_result_t *wifi_scan_result = NULL;

    if(code == LV_EVENT_CLICKED){
        if(0 == strcmp(txt, "back")){
            lv_obj_del(s_wifi_obj);
            s_wifi_obj = NULL;
        }else if(0 == strcmp(txt, "refresh")){
            moudule_wifi_scan_once();
            wifi_scan_result = module_wifi_get_scan_result();
            full_wifi_list_ui(s_wifi_list, wifi_scan_result);
        }else {

        }
    }
}
static void full_wifi_list_ui(lv_obj_t *wifi_list, wifi_scan_result_t *wifi_scan_result)
{
    if(NULL == wifi_list || NULL == wifi_scan_result){
        ESP_LOGE(TAG, "wifi_list or wifi_scan_result is NULL.");
        return;
    }
    
    lv_obj_clean(wifi_list);
    for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < wifi_scan_result->ap_count); i++) {
        lv_obj_t *wifi_btn = lv_obj_create(wifi_list);
        lv_obj_set_flex_flow(wifi_btn, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(wifi_btn, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_all(wifi_btn, 5, 0);
        lv_obj_set_size(wifi_btn, LV_PCT(75), 50);
        lv_obj_t *wifi_label = lv_label_create(wifi_btn);
        lv_label_set_text(wifi_label, (char *)wifi_scan_result->ap_info[i].ssid);
    }
}

void module_app_wifi_enter(lv_obj_t *parent)
{
    if(NULL == parent){
        ESP_LOGE(TAG, "parent is NULL.");
        return;
    }
    
    //扫描wifi
    moudule_wifi_scan_once();
    // module_wifi_fast_connect_wifi();

    s_wifi_obj = app_interface_management_create_page_parent_cont(parent);
    
    lv_obj_t *back_btn = lv_obj_create(s_wifi_obj);
    app_im_init_obj(back_btn, LV_OPA_60, 0x3C3D42);
    lv_obj_set_size(back_btn, 30, 50);
    lv_obj_set_pos(back_btn, 10, 15);
    lv_obj_set_style_radius(back_btn, 5, 0);
    lv_obj_add_event_cb(back_btn, _wifi_page_event, LV_EVENT_CLICKED, "back");

    lv_obj_t *refresh_btn = lv_obj_create(s_wifi_obj);
    app_im_init_obj(refresh_btn, LV_OPA_60, 0x3C3D42);
    lv_obj_set_size(refresh_btn, 30, 50);
    lv_obj_set_pos(refresh_btn, 10, 180);
    lv_obj_set_style_radius(refresh_btn, 5, 0);
    lv_obj_add_event_cb(refresh_btn, _wifi_page_event, LV_EVENT_CLICKED, "refresh");


    s_wifi_list = lv_obj_create(s_wifi_obj);
    app_im_init_obj(s_wifi_list, 0, 0);
    lv_obj_set_size(s_wifi_list, lv_disp_get_hor_res(NULL) - 15, lv_disp_get_ver_res(NULL));
    lv_obj_set_pos(s_wifi_list, 50, 0);
    lv_obj_set_flex_flow(s_wifi_list, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(s_wifi_list, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
    lv_obj_set_style_pad_row(s_wifi_list, 5, 0);
    lv_obj_set_style_pad_ver(s_wifi_list, 5, 0);

    wifi_scan_result_t *wifi_scan_result = NULL;
    wifi_scan_result = module_wifi_get_scan_result();
    full_wifi_list_ui(s_wifi_list, wifi_scan_result);
}